package com.eeepay.v2_library.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Spanned;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.eeepay.v2_library.dialog.CustomDialog;
import com.eeepay.v2_library.dialog.DialogUtil;
import com.eeepay.v2_library.util.ScreenSwitch;
import com.eeepay.v2_library.view.LeftRightText;

/**
 * Created by zw on 2016/2/26 0026.
 */
public abstract class ABBaseActivity extends FragmentActivity {

    protected Activity mContext;
    protected CustomDialog progressDialog;
    private Toast mToast;
    protected Bundle bundle;

    /**
     *
     * @return 布局id
     */
    protected abstract int getLayoutId();

    /**
     * 初始化控件
     */
    protected abstract void initWidget();

    /**
     * 事件绑定
     */
    protected abstract void eventOnClick();

    /**
     * 重写此方法，发起异步异步请求
     *
     * @param taskId
     *            异步请求任务id
     */
    protected void sendHttpRequest(int taskId) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
//		initSystemBar(mContext);
        setContentView(getLayoutId());
        bundle = getIntent().getExtras();
        initWidget();


        eventOnClick();
//        loginUtil = LoginUtil.getInstance();
//        loginUtil.addActivity(getLocalClassName(), this);
    }

    @Override
    protected void onStart() {
        super.onStart();
//        loginUtil.setTime(System.currentTimeMillis());
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
//        loginUtil.removeActivity(getLocalClassName());
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T getViewById(int id) {
        View view = findViewById(id);
        return (T) view;
    }

    public void showProgressDialog() {
        if (progressDialog == null) {
            progressDialog = DialogUtil.getHorizontalProgressDialog(mContext);
        }
        if (!isFinishing())
        progressDialog.show();
    }

    public void showProgressDialog(String string) {
        if (progressDialog == null) {
            progressDialog = DialogUtil.getHorizontalProgressDialog(mContext, string);
        }
        if (!isFinishing())//add by zhuangzeqin 2017年5月2日11:42:25  修复偶发奔溃的bug
        progressDialog.show();
    }

    public void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            if (!isFinishing())//add by zhuangzeqin 2017年5月2日11:42:25  修复偶发奔溃的bug
            progressDialog.dismiss();
        }
    }

    public void showToast(final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);
                } else {
                    mToast.setText(text);
                    mToast.setDuration(Toast.LENGTH_SHORT);
                }
                mToast.setGravity(Gravity.CENTER,0,0);
                mToast.show();
            }
        });
    }

    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

    public void onBackPressed() {
        cancelToast();
        super.onBackPressed();
    }

    public void goActivityForResult(Class<?> descClass, int requestCode){
        ScreenSwitch.switchActivity(mContext, descClass, null, requestCode);
    }

    public void goActivityForResult(Class<?> descClass, Bundle bundle, int requestCode){
        ScreenSwitch.switchActivity(mContext, descClass, bundle, requestCode);
    }

    public void goActivity(Class<?> descClass, Bundle bundle) {
        goActivityForResult(descClass, bundle, 0);
    }

    public void goActivity(Class<?> descClass) {
        goActivity(descClass, null);
    }

    public void setText(int viewId, String text) {
        TextView tv = getViewById(viewId);
        tv.setText(text);
    }

    public void setText(int viewId, Spanned text) {
        TextView tv = getViewById(viewId);
        tv.setText(text);
    }

    protected void setRightText(int viewId, CharSequence text){
        LeftRightText tv = getViewById(viewId);
        tv.setRightText(text);
    }

    protected void setRightText(int viewId, CharSequence text, int color){
        LeftRightText tv = getViewById(viewId);
        tv.setRightTextColor(text, color);
    }

    protected String getTextContent(int viewId){
        TextView tv = getViewById(viewId);
        return  tv.getText().toString().trim();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        TCAgent.onResume(this);
        // 极光推送点击了通知栏统计
//        JPushInterface.onResume(this);
    }
    @Override
    protected void onPause() {
        super.onPause();
//        TCAgent.onPause(this);
        // 极光推送点击了通知栏统计
//        JPushInterface.onPause(this);
    }
}

